<script setup>
import Info from './info.vue'
import {onMounted, ref} from 'vue'
import {pageApi,deleteApi} from "../../../api/course";
import {ElMessage, ElMessageBox} from "element-plus";
import {useCourseStore} from "../../../stores/course";
import {allApi} from "../../../api/coursetype";
import {allItemApi,saveItemApi,deleteItemApi} from "../../../api/courseitem";
const tableData = ref({})
const store=useCourseStore()
// 表单状态
const filterState = ref(true)
// 表单数据
const formData = ref({
  page: 1,
  size: 10,
  name: '',
  type: null
})

onMounted(async ()=>{
  loadData()
  if(store.types==null || store.types.length==0){
    const r=await allApi()
    store.types=r.data
  }
})

const loadData=()=>{
  pageApi(formData.value).then(r=>{
    tableData.value=r.data;
  })
}
// 编辑
const authEditRef = ref()
const onAuthEdit = (row) => {
  authEditRef.value.state = true
  // 回显
  if (row) {
    authEditRef.value.formData.id = row.id
    authEditRef.value.formData.ctId = row.ctId
    authEditRef.value.formData.imgUrl = row.imgUrl // 1-菜单， 2-按钮
    authEditRef.value.formData.info = row.info
    authEditRef.value.formData.score = row.score
    authEditRef.value.formData.teacher = row.teacher //1-缓存,0-不缓存
    authEditRef.value.formData.teacherInfo = row.teacherInfo
    authEditRef.value.formData.times = row.times
    authEditRef.value.formData.title = row.title
  }
}

// 权限删除
const onDelete = (row) => {
  ElMessageBox.confirm('确定删除该条数据吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    //接口请求 删除角色
    const  r=await deleteApi({id:row.id});
    if(r.code==0){
      ElMessage({
        type: 'success',
        message: '删除成功'})
        //重新加载请求
        loadData()
      }else {
        ElMessage.error(r.data)
      }
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '用户取消删除'
    })
  })
}
const addItem=ref(false)
const item=ref({})
const items=ref([])
const state=ref(false)
const onShowItem=async (row)=>{
  state.value=true
  item.value.courseId=row.id
  item.value.title=row.title
  loadItems()

}
const loadItems=async ()=>{
  const r=await allItemApi({cid:item.value.courseId})
  items.value=r.data
  console.log(items.value)
}
const uploadSuccess=(res)=>{
  item.value.resourceUrl=res.data
}
const onAdd=async ()=>{
  const r=await saveItemApi(item.value)
  if(r.code==0){
    ElMessage.success("新增课程资源成功！")
    loadItems()
    addItem.value=false
  }else {
    ElMessage.error(r.data)
  }
  item.value={courseId:item.value.courseId}
}
const onDelItem=(row)=>{
  ElMessageBox.confirm('确定删除该条数据吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const r=await deleteItemApi({id:row.id})
    if(r.code==0){
      ElMessage.success("删除课程资源成功")
      loadItems()
    }else {
      ElMessage.error(r.data)
    }
  })
}
</script>
<template>
  <Box>
    <!-- 逻辑层 -->
    <Info ref="authEditRef" @flush="loadData"/>
    <!-- 逻辑层 -->

    <!-- 筛选 -->
    <template #filter v-if="filterState">
      <el-form :inline="true" :model="formData">
        <el-form-item label="课程名称">
          <el-input placeholder="请输入课程名称" v-model="formData.name" />
        </el-form-item>
        <el-form-item label="课程类型">
          <el-select placeholder="请选择课程类型" v-model="formData.type"  style="width: 120px">
            <el-option v-for="t in store.types" :label="t.name" :value="t.id"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"> <span class="iconfont icon-sousuo"></span>搜索 </el-button>
          <el-button type="default"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </template>
    <!-- 筛选 -->

    <!-- 按钮组 -->
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="onAuthEdit">
          <span class="iconfont icon-jiahao"></span>添加
        </el-button>
      </el-col>

      <el-col :span="12" style="text-align: right">
        <el-button circle @click="filterState = !filterState">
          <span class="iconfont icon-sousuo"></span>
        </el-button>
        <el-button circle>
          <span class="iconfont icon-shuaxin"></span>
        </el-button>
      </el-col>
    </el-row>
    <!-- 按钮组 -->

    <el-table :data="tableData.data" :border="true" class="qfTable" row-key="id" default-expand-all>
      <el-table-column label="序号" prop="id" align="center"></el-table-column>
      <el-table-column label="名称" prop="title" align="center"></el-table-column>
      <el-table-column label="积分" prop="score" align="center"></el-table-column>
      <el-table-column label="老师" prop="teacher" align="center"></el-table-column>
      <el-table-column label="学习时长" prop="times" align="center"></el-table-column>
      <el-table-column label="封面图片" align="center">
        <template #default="scoped">
          <img :src="scoped.row.imgUrl" style="width: 100px;height: 100px">
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="ctime" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template #default="scoped">
          <el-button type="primary" link size="small" @click="onAuthEdit(scoped.row)">
            <span class="iconfont icon-bianji"></span>修改
          </el-button>
          <el-button type="primary" link size="small" @click="onDelete(scoped.row)">
            <span class="iconfont icon-shanchu"></span>删除
          </el-button>
          <el-button type="primary" link size="small" @click="onShowItem(scoped.row)">
            <span class="iconfont icon-shanchu"></span>查看资源
          </el-button>

        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
        v-model:current-page="formData.page"
        v-model:page-size="formData.size"
        :page-sizes="[10, 20, 30, 40]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
        @size-change="loadData"
        @current-change="loadData"
    />

<!--    课程资源-->
    <el-dialog
        v-model="state"
        title="课程资源列表"
        width="900"
        :before-close="() => (state = false)"
    >
      <!-- 按钮组 -->
      <el-row>
        <el-col :span="12">
          <el-button type="primary" @click="addItem=true">
            <span class="iconfont icon-jiahao"></span>添加课程资源
          </el-button>
        </el-col>
      </el-row>
      <!-- 按钮组 -->

      <el-table :data="items" :border="true" class="qfTable" row-key="id" default-expand-all>
        <el-table-column label="序号" prop="no" align="center" width="60"></el-table-column>
        <el-table-column label="资源名称" prop="name" align="center"></el-table-column>
        <el-table-column label="资源类型" align="center" width="100">

          <template #default="scoped">
            <el-tag type="primary" v-if="scoped.row.type==1">视频</el-tag>
            <el-tag type="success" v-else-if="scoped.row.type==2">三分屏</el-tag>
            <el-tag type="danger" v-else>PPT文档</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="资源链接" align="center">
          <template #default="scoped">
            <a :href="scoped.row.resourceUrl">点击预览</a>
          </template>
        </el-table-column>
        <el-table-column label="学习时长" prop="times" align="center" width="100"></el-table-column>

        <el-table-column label="创建时间" prop="ctime" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="120" fixed="right">
          <template #default="scoped">
            <el-button type="primary" link size="small" @click="onDelItem(scoped.row)">
              <span class="iconfont icon-shanchu"></span>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--  新增资源  -->
      <el-dialog
          v-model="addItem"
          title="新增课程资源"
          width="500"
          :before-close="() => (addItem = false)"
      >
        <!-- 内容 -->
        <el-form
            size="large"
            label-width="80"
            :model="item"
            :rules="formRules"
            ref="formRef"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="课件名称">
                <el-input placeholder="请输入课件名称" v-model="item.name" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="课件类型">
                <el-select placeholder="课件类型" v-model="item.type">
                  <el-option label="视频" value="1"/>
                  <el-option label="三分屏" value="2"/>
                  <el-option label="PPT文档" value="3"/>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="学习时长">
                <el-input type="number" placeholder="请输入学习时长" v-model="item.times" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
<!--              <el-form-item label="资源序号">-->
<!--                <el-input type="number" placeholder="请输入资源序号" v-model="item.no" />-->
<!--              </el-form-item>-->
            </el-col>
          </el-row>
          <el-form-item label="资源上传">
            <el-upload
                class="avatar-uploader"
                action="http://localhost:8080/api/resource/upload"
                :show-file-list="false"
                :on-success="uploadSuccess"
            >
              <p v-if="item.resourceUrl">{{item.resourceUrl}}</p>
              <el-icon v-else class="avatar-uploader-icon">+</el-icon>
            </el-upload>
          </el-form-item>

          <el-form-item label="课件简介">
            <el-input  type="textarea" :rows="3" v-model="item.info"/>
          </el-form-item>

          <el-form-item label="">
            <el-button type="primary" @click="onAdd">新增课件</el-button>
            <el-button type="default" @click="addItem = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>

    </el-dialog>
  </Box>
</template>

<style lang="scss" scoped></style>
